package com.bye.xiaomi_mall.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.bye.xiaomi_mall.domain.TdGoods;
import com.bye.xiaomi_mall.domain.TdGoodsCategory;
import com.bye.xiaomi_mall.domain.TdGoodsSpecifications;
import com.bye.xiaomi_mall.domain.TdOrderDetail;
import com.bye.xiaomi_mall.mapper.TdGoodsCategoryMapper;
import com.bye.xiaomi_mall.mapper.TdGoodsMapper;
import com.bye.xiaomi_mall.mapper.TdGoodsSpecificationsMapper;
import com.bye.xiaomi_mall.service.ITdGoodsService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@Service
public class TdGoodsServiceImpl implements ITdGoodsService {
    @Autowired
    private TdGoodsMapper tdGoodsMapper;

    @Autowired
    private TdGoodsCategoryMapper tdGoodsCategoryMapper;

    @Autowired
    private TdGoodsSpecificationsMapper tdGoodsSpecificationsMapper;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public TdGoods selectTdGoodsById(Long id) {
        TdGoods goods = tdGoodsMapper.selectTdGoodsById(id);
        String features = goods.getFeatures();
        List<String> list = Arrays.asList(features.split("[,，_]"));
        List<TdGoodsSpecifications> specificationsList = new ArrayList<>();
        for (String spId : list) {
            TdGoodsSpecifications tdGoodsSpecifications = tdGoodsSpecificationsMapper.selectTdGoodsSpecificationsById(Long.valueOf(spId));
            if (null != tdGoodsSpecifications) {
                String value = tdGoodsSpecifications.getValue();
                tdGoodsSpecifications.setValueList(Arrays.asList(value.split("[,，_]")));
                String addMoney = tdGoodsSpecifications.getAddMoney();
                tdGoodsSpecifications.setAddMoneyList(Arrays.asList(addMoney.split("[,，_]")));
                specificationsList.add(tdGoodsSpecifications);
            }
        }
        goods.setTdGoodsSpecificationsList(specificationsList);

        return goods;
    }

    /**
     * 查询商品列表
     *
     * @param tdGoods 商品
     * @return 商品
     */
    @Override
    public List<TdGoods> selectTdGoodsList(TdGoods tdGoods) {
        List<TdGoods> tdGoodsList = tdGoodsMapper.selectTdGoodsList(tdGoods);
        for (TdGoods goods : tdGoodsList) {
            String value = goods.getFeatures();
            List<String> list = Arrays.asList(value.split("[,，_]"));
            goods.setFeaturesList(list);
        }

        return tdGoodsList;
    }

    /**
     * 新增商品
     *
     * @param tdGoods 商品
     * @return 结果
     */
    @Override
    public int insertTdGoods(TdGoods tdGoods) {
        tdGoods.setCreateTime(DateUtils.getNowDate());
        return tdGoodsMapper.insertTdGoods(tdGoods);
    }

    /**
     * 修改商品
     *
     * @param tdGoods 商品
     * @return 结果
     */
    @Override
    public int updateTdGoods(TdGoods tdGoods) {
        return tdGoodsMapper.updateTdGoods(tdGoods);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteTdGoodsByIds(Long[] ids) {
        return tdGoodsMapper.deleteTdGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteTdGoodsById(Long id) {
        return tdGoodsMapper.deleteTdGoodsById(id);
    }

    @Override
    public List<TdGoodsCategory> selectGoodsType() {

        return tdGoodsCategoryMapper.selectGoodsType();
    }

    @Override
    public List<TdGoodsSpecifications> selectGoodsSp() {
        List<TdGoodsSpecifications> tdGoodsSpecificationsList = tdGoodsSpecificationsMapper.selectGoodsSp();
        for (TdGoodsSpecifications tdGoodsSpecifications : tdGoodsSpecificationsList) {
            String value = tdGoodsSpecifications.getValue();
            List<String> resultList = Arrays.asList(value.split("[,，_]"));
            tdGoodsSpecifications.setValueList(resultList);
        }
        return tdGoodsSpecificationsList;
    }

    @Override
    public List<TdGoods> selectGoods() {

        return tdGoodsMapper.selectGoodsType();
    }

    @Override
    public List<TdGoods> selectMainGoods(Long typeId, String isMainShow) {
        return tdGoodsMapper.selectMainGoods(typeId, isMainShow);
    }

    @Override
    public List<TdGoods> selectMainGoods(Long typeId) {
        return tdGoodsMapper.selectMainGoods2(typeId);
    }

    @Override
    public List<TdGoods> selectMainGoods() {
        return tdGoodsMapper.selectMainGoods();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateGoodsqlse(List<TdOrderDetail> orderDetailList) {
        int i = 0;
        for (TdOrderDetail orderDetail : orderDetailList) {
            Long goodNum = orderDetail.getGoosNum();
            Long goodsId = orderDetail.getGoodsId();
            int l = tdGoodsMapper.updateGoodsInventory(goodsId, goodNum);

            if (l == 0) {
                return -1;
            }
            i += l;

        }
        return i;
    }

}
